<template>
    <rx-dialog @handOk="submitForm" @cancel="cancel" order="buttom" btnalign="center">
        <rx-fit>
            <div style="height:100%;width:100%;padding:20px;">
                <a-form-model ref="form" :model="form" :rules="rules">
                    <a-form-model-item :label="i18n('addGroupPage.addGroup','授权组')" :labelCol="labelCol1" :wrapperCol="wrapperCol1">
                        <rx-text-list  v-model="handlerGroups" valuefield="groupId" textfield="name" @click="handExecutors"  :showclose="false">
                            <a-icon type="edit"  />
                        </rx-text-list>
                    </a-form-model-item>
                    <a-form-model-item :label="i18n('addGroupPage.desc','备注')" :labelCol="labelCol1" :wrapperCol="wrapperCol1" prop="opinion">
                        <rx-textarea-ext v-model="form.opinion" :auto-size="{'minRows':4}"></rx-textarea-ext>
                    </a-form-model-item>
                </a-form-model>
            </div>
        </rx-fit>
    </rx-dialog>
</template>

<script>
    import {BaseFormModel, RxDialog,Util,Dialog} from '@lowcode/jpaas-common-lib';
    import BpmInstPermissionApi from "@/api/bpm/core/bpmInstPermission";
    import rxTextareaExt from "../comps/rxTextareaExt";

    export default {
        name: "accessAddGroup",
        props: {
            instId: String,
            layerid: String,
            lydata: Object,
            destroy: Function
        },
        mixins: [BaseFormModel],
        components: {
            RxDialog,
            rxTextareaExt
        },
        data() {
            return {
                rules: {
                    opinion: [{required: true, message: this.i18n('addGroupPage.msg.validDesc','请输入备注'), trigger: 'change'}]
                },
                handlerGroups: [],
                form: {
                    opinion: ""
                }
            }
        },
        created() {
            this.init();
        },
        methods: {
            i18n(name,text,key){
                return this.$ti18(name,text,"BpmInstPermissionManagement",key);
            },
            init() {
                this.form.instId = this.instId;
            },
            closeWindow() {
                Util.closeWindow(this, 'cancel');
            },
            handExecutors(){
                let _data = this.handlerGroups.map(item=>{
                    return {
                        groupId:item.groupId,
                        name:item.name
                    }
                })
                var conf = {
                    curVm: this,
                    data: {
                        single: false,
                        initGroups: _data
                    },
                    widthHeight: ['1024px', '600px']
                };
                Dialog.openGroupDialog(conf, function (self, groups) {
                    var ary=[];
                    for (var i = 0; i < groups.length; i++) {
                        var obj = groups[i];
                        var o={groupId:obj.groupId,name:obj.name};
                        ary.push(o);
                    }
                    self.handlerGroups=ary;
                });
            },
            submitForm(e) {
                var self_ = this;
                this.$refs.form.validate(valid => {
                    if (!valid) {
                        e.loading = false;
                        return;
                    }
                    if (self_.handlerGroups.length == 0) {
                        e.loading = false;
                        self_.$message.error(self_.i18n("addGroupPage.msg.validGroup","请选择授权组"));
                        return;
                    }
                    self_.form.groupIds = self_.handlerGroups.map(item => {
                        return item.groupId;
                    }).join(",")

                    BpmInstPermissionApi.savePermissionGroup(self_.form).then(resp => {
                        e.loading = false;
                        if (resp.success) {
                            Util.closeWindow(self_, "ok");
                        }
                    })
                })
            }
        }
    }
</script>

<style scoped>

</style>